package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;

/**
 * @Description:
 * @BelongsProject: sky-take-out
 * @BelongsPackage: com.sky.service.impl
 * @Author: zhouzhijie
 * @CreateTime: 2023-11-16  20:15
 * @Version: 1.0
 */
@Service
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private DishMapper dishMapper;

    /**
     * @description: 菜品分页查询
     * @author: zhouzhijie
     * @date: 2023/11/16 20:20
     * @param: [setmealDTO]
     * @return:
     **/
    @Override
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
         PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
         Page<SetmealVO> page = setmealMapper.pageQuery(setmealPageQueryDTO);

        return new PageResult(page.getTotal(),page.getResult());
    }

    /**
     * @description: 新增套餐
     * @author: zhouzhijie
     * @date: 2023/11/17 10:34
     * @param: [setmealDTO]
     * @return: 
     **/
    @Override
    @Transactional
    public void saveWithDish(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        //新增套餐默认停售状态
        setmeal.setStatus(StatusConstant.DISABLE);
        //向套餐表插入一条数据
        setmealMapper.insert(setmeal);
        //获取生成的套餐表id
        Long setmealId = setmeal.getId();
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        if (setmealDishes != null && setmealDishes.size()>0){
            setmealDishes.forEach(setmealDish -> {
                setmealDish.setSetmealId(setmealId);
            });
        }
        //保存套餐与菜品的关系
        //向套餐菜品表表插入n条数据
        setmealDishMapper.insertBatch(setmealDishes);

    }

    /**
     * @description: 套餐批量删除
     * @author: zhouzhijie
     * @date: 2023/11/17 14:46
     * @param: [ids]
     * @return:
     **/
    @Override
    @Transactional
    public void deleteBatch(List<Long> ids) {
        //判断当前套餐是否能删除，是否在起售中
        for (Long id : ids) {
            //根据主键查询套餐
            Setmeal setmeal = setmealMapper.getById(id);
            if (setmeal.getStatus() == StatusConstant.ENABLE){
                //当前套餐在起售中，不能删除
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        }
        //批量删除套餐表数据
        setmealMapper.deleteBatchByIds(ids);
        //根据套餐ids批量删除套餐菜品关系表数据
        setmealDishMapper.deleteBatchByIds(ids);
    }

    /**
     * @description: 根据id查询套餐和套餐菜品关系
     * @author: zhouzhijie
     * @date: 2023/11/17 17:30
     * @param: [id]
     * @return:
     **/
    @Override
    public SetmealVO getByIdWithDish(Long id) {
        //根据id查询套餐数据
        Setmeal setmeal = setmealMapper.getById(id);
        //根据套餐id查询套餐菜品关系表数据
        List<SetmealDish> setmealDishes = setmealDishMapper.getBySetmealhId(id);
        //将查询到的数据封装到Vo
        SetmealVO setmealVO = new SetmealVO();
        BeanUtils.copyProperties(setmeal,setmealVO);
        setmealVO.setSetmealDishes(setmealDishes);
        return setmealVO;
    }

    /**
     * @description: 修改套餐
     * @author: zhouzhijie
     * @date: 2023/11/18 16:24
     * @param: [setmealDTO]
     * @return:
     **/
    @Override
    @Transactional
    public void update(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        //因为更新需要修改时间和操作人，setmealDTO没有在这两项属性
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmeal.setUpdateUser(BaseContext.getCurrentId());
        //修改套餐表一条数据
        setmealMapper.update(setmeal);
        //获取套餐表id
        Long setmealId = setmealDTO.getId();
        //删除套餐菜品表数据
        setmealDishMapper.deleteBySetmealId(setmealId);
        //保存套餐菜品关系表n条数据
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        if (setmealDishes != null && setmealDishes.size() > 0){
            setmealDishes.forEach(setmealDish -> {
                //新增的套餐菜品关系没有setmealId关联
                setmealDish.setSetmealId(setmealId);
            });
            setmealDishMapper.insertBatch(setmealDishes);
        }
    }

    /**
     * @description: 套餐起售、停售
     * @author: zhouzhijie
     * @date: 2023/11/18 17:38
     * @param: [status, id]
     * @return:
     **/
    @Override
    public void startOrstop(Integer status, Long id) {
        //起售套餐时，判断套餐内是否有停售菜品
        if (status ==  StatusConstant.ENABLE){
            //根据套餐id获取对应的菜品数据
            List<Dish> dishes = dishMapper.getBySetmealId(id);
            if (dishes != null && dishes.size() > 0){
                dishes.forEach(dish -> {
                    if (StatusConstant.DISABLE == dish.getStatus()){
                        //菜品停售
                        throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ENABLE_FAILED);
                    }
                });
            }
        }
        Setmeal setmeal = new Setmeal();
        setmeal.setId(id);
        setmeal.setStatus(status);
        setmealMapper.update(setmeal);
    }

    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }
}
